<template>
  <a-spin :spinning="confirmLoading">
    <a-form-model ref="form" :model="model" :rules="validatorRules" class="cus-form2">
      <a-row :gutter="20">
        <a-col :span="3" style="padding-right: 0">
          <a-form-model-item
            :label="$t('最大储值额度')"
            :labelCol="labelCol2"
            :wrapperCol="wrapperCol2"
            style="width: 100%"
          >
          </a-form-model-item>
        </a-col>
        <a-col :span="4" style="padding-left: 0; padding-right: 0">
          <a-form-model-item :label="$t('代币')" :labelCol="labelCol1" :wrapperCol="labelCol1" prop="maxCoin">
            <a-input-number :min="0" v-model="model.maxCoin" :placeholder="$t('请输入代币额度')" style="width: auto" />
          </a-form-model-item>
        </a-col>
        <a-col :span="4" style="padding-left: 0; padding-right: 0">
          <a-form-model-item :label="$t('彩票')" :labelCol="labelCol1" :wrapperCol="labelCol1" prop="maxLottery">
            <a-input-number
              :min="0"
              v-model="model.maxLottery"
              :placeholder="$t('请输入彩票额度')"
              style="width: auto"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="4" style="padding-left: 0; padding-right: 0">
          <a-form-model-item :label="$t('A币')" :labelCol="labelCol1" :wrapperCol="labelCol1" prop="maxPoints">
            <a-input-number :min="0" v-model="model.maxPoints" :placeholder="$t('请输入A币额度')" style="width: auto" />
          </a-form-model-item>
        </a-col>
        <a-col :span="4" style="padding-left: 0; padding-right: 0">
          <a-form-model-item :label="$t('A票')" :labelCol="labelCol1" :wrapperCol="labelCol1" prop="maxAlottery">
            <a-input-number
              :min="0"
              v-model="model.maxAlottery"
              :placeholder="$t('请输入A票额度')"
              style="width: auto"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="4" style="padding-left: 0; padding-right: 0">
          <a-form-model-item style="margin-left: 10px; margin-top: -2px; white-space: nowrap">
            <a-button type="primary" @click="submitForm" v-if="!disableSubmit">{{ $t('保存') }}</a-button>
            <span style="color: orange">{{ $t('（0表示不限制）') }}</span>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="20">
        <a-col :span="3" style="padding-right: 0">
          <a-form-model-item
            :label="$t('会员过户')"
            :labelCol="labelCol2"
            :wrapperCol="wrapperCol2"
            style="width: 100%"
          >
          </a-form-model-item>
        </a-col>
        <a-col :span="4" style="padding-left: 0; padding-right: 0">
          <a-form-model-item
            :label="$t('过户比例')"
            :labelCol="labelCol1"
            :wrapperCol="labelCol1"
            prop="transferRate"
          >
            <div style="display: flex; align-items: center">
              <a-input-number v-model="model.transferRate" :placeholder="$t('请输入过户比例')" /> %
            </div>
          </a-form-model-item>
        </a-col>
        <a-col :span="15" style="padding-left: 0; padding-right: 0">
          <a-form-model-item>
            <a-button type="primary" @click="submitForm" v-if="!disableSubmit" style="margin-left: 10px; margin-top: -2px">{{
              $t('保存')
            }}</a-button>
            <span style="color: orange">{{ $t('（转入账户的存卡值=转出账户的销币值*过户比例）') }}</span>
          </a-form-model-item>
        </a-col>
        <a-col offset="3" :span="20" style="padding-left: 0; padding-right: 0">
          <a-form-model-item :labelCol="wrapperCol2" :wrapperCol="labelCol2">
            <span>{{$t('分享类型')}}：</span>
            <a-checkbox-group v-model="selectedShareItems">
              <a-checkbox v-for="item in allShareItems" :key="item.acctType" :value="item.acctType">{{ item.acctType_dictText }}</a-checkbox>
            </a-checkbox-group>
            <a-button type="primary" @click="submitForm2" v-if="!disableSubmit" style="margin-left: 10px; margin-top: -2px">{{
              $t('保存')
            }}</a-button>
            <span style="color: orange">{{ $t('（关闭的类型吧台和小程序上将不允许分享）') }}</span>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="20">
        <a-col :span="3" style="padding-right: 0">
          <a-form-model-item
            :labelCol="labelCol2"
            :wrapperCol="wrapperCol2"
            style="width: 100%"
          >
          <span slot="label">
            {{$t('会员存票')}}
            <a-tooltip :title="$t('可独立控制存票业务的存票方式，不开启的存票类型则吧台存票业务中不显示')">
              <a-icon type="question-circle-o" />
            </a-tooltip>
          </span>
          </a-form-model-item>
        </a-col>
        <a-col :span="16" style="padding-left: 0; padding-right: 0">
          <a-form-model-item :labelCol="wrapperCol2" :wrapperCol="labelCol2">
            <a-checkbox-group v-model="selectedItems">
              <a-checkbox v-for="item in options" :key="item.value" :value="item.value">{{ item.title }}</a-checkbox>
            </a-checkbox-group>
            <a-button type="primary" @click="submitForm1" v-if="!disableSubmit" style="margin-left: 10px; margin-top: -2px">{{
              $t('保存')
            }}</a-button>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </a-spin>
</template>

<script>
import { httpAction, getAction, postAction } from '@/api/manage'
import { initDictOptions } from '@/components/dict/JDictSelectUtil'

export default {
  name: 'RuleMoreForm',
  components: {},
  data() {
    return {
      disableSubmit: false,
      defmodel: {},
      model: {},
      labelCol1: {
        xs: { span: 24 },
        sm: { span: 12 },
      },
      wrapperCol1: {
        xs: { span: 24 },
        sm: { span: 12 },
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 24 },
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 0 },
      },
      confirmLoading: false,
      validatorRules: {
        transferRate: [{ required: true, message: this.$t('请输入过户比例') }],
        maxCoin: [{ required: true, message: this.$t('请输入代币额度') }],
        maxLottery: [{ required: true, message: this.$t('请输入彩票额度') }],
        maxPoints: [{ required: true, message: this.$t('请输入A币额度') }],
        maxAlottery: [{ required: true, message: this.$t('请输入A票额度') }],
      },
      url: {
        add: '/member/bisMemberRule/add',
        edit: '/member/bisMemberRule/edit',
        queryById: '/member/bisMemberRule/queryById',
      },
      allItems: [],
      selectedItems: [],
      options: [],
      allShareItems: [],
      selectedShareItems: [],
    }
  },
  created() {},
  methods: {
    async edit(record) {
      this.selectedItems = []
      await this.getType0()
      this.selectedShareItems = []
      this.defmodel = Object.assign({}, record)
      this.model = Object.assign({}, record)
      await this.getShareTypes()
      await this.getMemberLtypes()
      this.visible = true
    },
    getType0(){
      let that = this;
      return new Promise(resolve=>{
        initDictOptions("save_lottery_method").then((res) => {
          if (res.success) {
            that.options = res.result;
          }
          resolve()
        })
      })
    },
    getMemberLtypes(){
      let that = this;
      return new Promise(resolve=>{
        getAction("/member/bisMemberRuleSave/list", {memberRuleId: that.model.id, pageSize: 20}).then((res) => {
          if (res.success) {
            if(res.result.records.length>0){
              that.allItems = res.result.records
              that.selectedItems = res.result.records.filter(r => {return r.status == 1}).map(r => { return r.type })
            }
          }
          resolve()
        })
      })
    },
    getShareTypes(){
      let that = this;
      return new Promise(resolve=>{
        getAction("/member/bisMemberRuleAcct/list", {memberTypeId: that.model.id, setCode: 'MemberTransfer', pageSize: 20}).then((res) => {
          if (res.success) {
            if(res.result.records.length>0){
              that.allShareItems = res.result.records
              that.selectedShareItems = res.result.records.filter(r => {return r.state == 1}).map(r => { return r.acctType })
            }
          }
          resolve()
        })
      })
    },
    submitForm2(){
      let that = this, model = [];
      that.allShareItems.forEach(a => {
        a.state = 0
        let x = that.selectedShareItems.findIndex(s=>{
          return s == a.acctType
        })
        if(x > -1){
          model.push(Object.assign({}, a, { state: 1 }))
        }else{
          model.push(Object.assign({}, a))
        }
      })
      postAction('/member/bisMemberRuleAcct/editBatch', model).then(res=>{
        if(res.success){
          that.$message.success(res.message)
        }else{
          that.$message.error(res.message)
        }
      })
    },
    submitForm1(){
      let that = this, model = [];
      that.allItems.forEach(a => {
        a.status = 0
        let x = that.selectedItems.findIndex(s=>{
          return s == a.type
        })
        if(x > -1){
          model.push(Object.assign({}, a, { status: 1 }))
        }else{
          model.push(Object.assign({}, a))
        }
      })
      postAction('/member/bisMemberRuleSave/updateBatch', model).then(res=>{
        if(res.success){
          that.$message.success(res.message)
        }else{
          that.$message.error(res.message)
        }
      })
    },
    submitForm() {
      let that = this
      // 触发表单验证
      that.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!that.model.id) {
            httpurl += that.url.add
            method = 'post'
          } else {
            httpurl += that.url.edit
            method = 'put'
          }
          httpAction(httpurl, that.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
<style>
.cus-form2.ant-form .ant-form-item {
  margin-right: 4px;
  margin-bottom: 14px;
}
.cus-form2.ant-form .ant-form-item-with-help {
  margin-bottom: 14px;
}
</style>